#include "teacherwidget.h"
#include "ui_teacherwidget.h"
#include <QMessageBox>

TeacherWidget::TeacherWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::TeacherWidget)
{
    ui->setupUi(this);
    this->setWindowTitle("教师端");
    this->model=new QStandardItemModel;
    this->setM=new modelSet;
    this->ui->tableView->setModel(model);
    this->file1=new file;
    this->submitCourse=new teacherSubmitCourse;
    ui->tableView->setSelectionMode(QAbstractItemView::SingleSelection);
    ui->tableView->setSelectionBehavior(QAbstractItemView::SelectRows);
    connect(ui->tableView->horizontalHeader(),SIGNAL(sectionDoubleClicked(int)),this, SLOT(sortView(int)));
    this->ui->oldPassword->hide();
    this->ui->newPassword->hide();
    this->ui->btn_confirm->hide();
    this->ui->btn_quit->hide();
    this->ui->btn_sort->hide();
    this->ui->box->hide();
}

TeacherWidget::~TeacherWidget()
{
    delete ui;
}

void TeacherWidget::getLoginId(QString loginId)
{
    this->loginId=loginId;
}

void TeacherWidget::setTeaObject()
{
    QStringList teacherLine;
    QStringList myCourse;
    this->file1->readFile("teacher.csv",teacherLine);
    for(int i=0;i<teacherLine.length();i++)
    {
        QString line=teacherLine.at(i);
        line=line.trimmed();
        QStringList linesplit=line.split(",");
        if(loginId==linesplit.at(1))
        {
            for(int j=5;j<linesplit.length();j++)
            {
                myCourse.append(linesplit.at(j));
            }
            this->teaInfo=new teacher;
            this->teaInfo->loadScore();
            this->teaInfo->setInfo(linesplit.at(0),linesplit.at(1),linesplit.at(2),linesplit.at(3),linesplit.at(4),myCourse);
            this->teaInfo->loadInfo();
        }
    }
    this->ui->teaName->setText("欢迎"+this->teaInfo->getName()+"老师登陆系统！");
}

void TeacherWidget::on_btn_exit_clicked()
{
    this->teaInfo->saveAllInfo();
    this->close();
}

void TeacherWidget::on_btn_check_myinfo_clicked()
{
    this->model->clear();
    this->teaInfo->checkMyInfo(this->model,0);
    this->ui->btn_sort->hide();
    this->ui->box->hide();
}

void TeacherWidget::on_btn_check_score_clicked()
{
    this->ui->btn_sort->show();
    this->ui->box->show();
    this->model->clear();
    this->teaInfo->checkScore(this->model);
}

void TeacherWidget::on_btn_sort_clicked()
{
    int index = ui->box->currentIndex();
    if (index == 0)
    {
        this->model->clear();
        this->teaInfo->sortScoreUp(this->model);
    }
    else if (index == 1)
    {
        this->model->clear();
        this->teaInfo->sortScoreDown(this->model);
    }
}

void TeacherWidget::on_btn_check_student_info_clicked()
{
    int row=this->ui->tableView->currentIndex().row();
    if(row!=-1)
    {
        QModelIndex index=this->model->index(row,0);
        QString selectId=model->data(index).toString();
        this->teaInfo->loadMyStudent(selectId);

        this->model->clear();
        this->teaInfo->checkMyStudent(model);
    }
    else
    {
        QMessageBox::critical(this,"错误","请先选择相应课程！","确认");
    }
    this->ui->btn_sort->hide();
    this->ui->box->hide();
}

void TeacherWidget::on_btn_check_mycourse_clicked()
{
    this->model->clear();
    this->teaInfo->checkMyCourse(model);
    this->ui->btn_sort->hide();
    this->ui->box->hide();
}

void TeacherWidget::on_btn_submit_course_clicked()
{
    this->submitCourse->show();
    //this->submitCourse->exec();
    QStringList Course=this->submitCourse->getInfo();
    if(Course.at(0)!="0")
    {
        this->waitToLoadCourse=new course;
        this->waitToLoadCourse->setCourseInfo("","",Course.at(0),"",Course.at(1),Course.at(3),this->teaInfo->getCollege(),this->teaInfo->getName(),this->teaInfo->getLevel(),Course.at(4),Course.at(2),Course.at(5),"0");
        this->teaInfo->submitCourse(*waitToLoadCourse);
        this->submitCourse->clearUi();
    }
    this->ui->btn_sort->hide();
    this->ui->box->hide();
}

void TeacherWidget::on_btn_quit_clicked()
{
    this->ui->oldPassword->hide();
    this->ui->newPassword->hide();
    this->ui->btn_confirm->hide();
    this->ui->btn_quit->hide();
}

void TeacherWidget::on_btn_modify_password_clicked()
{
    this->ui->oldPassword->show();
    this->ui->newPassword->show();
    this->ui->btn_confirm->show();
    this->ui->btn_quit->show();
    this->ui->btn_sort->hide();
    this->ui->box->hide();
}

void TeacherWidget::on_btn_confirm_clicked()
{
    QString oldPassword=this->ui->oldPassword->text();
    QString newPassword=this->ui->newPassword->text();
    if(this->teaInfo->modifyPassword(oldPassword,newPassword))
    {
        if(QMessageBox::information(this,"修改成功！","密码修改成功","确认")==0)
        {
            this->ui->btn_confirm->hide();
            this->ui->btn_quit->hide();
            this->ui->oldPassword->hide();
            this->ui->newPassword->hide();
        }
    }
    else
    {
        QMessageBox::critical(this,"修改失败！","旧密码输入错误或新密码不足六位！","确认");
    }
}

void TeacherWidget::sortView(int a)
{
    if(flag)
        this->setM->quickSort(0, model->rowCount()-1,a,model);
    else
        this->setM->quickSort_1(0, model->rowCount()-1,a,model);
    flag=!flag;
}


